package websocket

import (
	"encoding/json"
	"iron_man_comm/src/common/log"
)

type MotorControl struct {
	Cmd  string           `json:"cmd"`
	Data MotorControlData `json:"data"`
}

type MotorControlData struct {
	ControlType uint8  `json:"control_type"`
	MotorType   uint8  `json:"motor_type"`
	Value       uint16 `json:"value"`
}

/**
{
    "cmd": "motor_control",
    "data": {
        //控制标识位，true表示控制有效，false表示不控制
        "windFlag": true, //风机控制
		"edgeBrushFlag": true, //边刷控制
		"rollBrushFlag": true, //滚刷控制
		"leftMopFlag": true, //左抹布控制
		"rightMopFlag": true, //右抹布控制
		"waterPumpFlag": true, //水泵控制
        //电机属性，只有标识为true时有效
        "windGear": 0,//风机档位
        "edgeBrushVoltage": 0,//边刷电压，单位：mV
        "rollBrushVoltage": 0,//滚刷电压，单位：mV
        "leftMopVoltage": 0,//左抹布盘电压，单位：100mV
        "leftMopRunTime": 0,//左抹布盘运行时间，单位：s
        "rightMopVoltage": 0,//右抹布盘电压，单位：mV
        "rightMopRunTime": 0,//右抹布盘运行时间，单位：s
        "waterPump": 0//水泵档位
    }
}
*/
func (s *IotServer) processCleanMotorControlData(message string) {
	var msg ControlCleanMotor
	err := json.Unmarshal([]byte(message), &msg)
	if err != nil {
		log.Error("ws server process control clean motor data fail, msg: %s", message)
		return
	}
	go s.controlCleanMotor(msg)
}

/**
{
    "cmd": "device_extend",
    "data": {
        //电机标识位,true表示控制有效，false表示不控制
        "rollBrushExtendFlag": true,//滚刷升降
        "edgeBrushExtendFlag": true,//边刷外摆
        "mopExtendFlag": true,//抹布外摆

        "rollBrushCmd": 0,//滚刷升降控制
        "edgeBrushCmd": 0,//边刷外摆控制
        "mopCmd": 0//抹布盘外摆控制
    }
}
*/
func (s *IotServer) processExtendMotorControlData(message string) {
	var msg ControlExtendMotor
	err := json.Unmarshal([]byte(message), &msg)
	if err != nil {
		log.Error("ws server process control clean motor data fail, msg: %s", message)
		return
	}
	go s.controlExtendMotor(msg)
}

/**
{
    "cmd": "light_control",
    "data": {
        "value": 0	//uint8，控制值
    }
}
*/
func (s *IotServer) processLightControlData(message string) {
	var msg ControlLight
	err := json.Unmarshal([]byte(message), &msg)
	if err != nil {
		log.Error("ws server process control light data fail, msg: %s", message)
		return
	}
	go s.controlLight(msg)
}

/**
{
    "cmd": "sensor_control",
    "data": {
        //传感器标识位,true表示控制有效，false表示不控制
        "downViewFlag": true,//下视
        "ultrasonicFlag": true,//超声
        "chargeFlag": true,//充电
        //控制类型，对应标志位为true时有效
        "downViewSwitch": 0,//下视
        "ultrasonicSwitch": 0,//超声
        "chargeSwitch": 0//充电
    }
}
*/
func (s *IotServer) processSensorControlData(message string) {
	var msg ControlSensor
	err := json.Unmarshal([]byte(message), &msg)
	if err != nil {
		log.Error("ws server process sensor data fail, msg: %s", message)
		return
	}
	go s.controlSensor(msg)
}

/**
{
    "cmd": "mcu_poweroff",
    "data": {
        "cmd": 0 //控制类型，1：关机，2：重启
    }
}
*/
func (s *IotServer) processMCUPowerOffData(message string) {
	var msg ControlMcuPower
	err := json.Unmarshal([]byte(message), &msg)
	if err != nil {
		log.Error("ws server process mcu power off data fail, msg: %s", message)
		return
	}
	go s.controlMCUPower(msg)
}
